package cn.itrip.controller;

import cn.itrip.beans.dto.Dto;
import cn.itrip.beans.pojo.ItripAreaDic;
import cn.itrip.beans.pojo.ItripImage;
import cn.itrip.beans.vo.ItripAreaDicVO;
import cn.itrip.beans.vo.ItripImageVO;
import cn.itrip.beans.vo.hotel.HotelVideoDescVO;
import cn.itrip.common.DtoUtil;
import cn.itrip.common.EmptyUtils;
import cn.itrip.service.itripAreaDic.ItripAreaDicService;
import cn.itrip.service.itripHotel.ItripHotelService;
import cn.itrip.service.itripImage.ItripImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2020/3/17 21:28
 * @Version: 1.0
 */
@Controller
@RequestMapping("/api/hotel")
public class HotelController {

    @Resource
    private ItripAreaDicService itripAreaDicService;
    @Resource
    private ItripImageService itripImageService;
    @Resource
    private ItripHotelService itripHotelService;

    /**
     * 查询国内、国外的热门城市(1:国内 2:国外)
     * @param type
     * @return
     */
    @RequestMapping(value = "/queryhotcity/{type}",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<ItripAreaDicVO> queryHotCity(@PathVariable Integer type){

        if(type == null || type == 0){
            return DtoUtil.returnFail("国家type类型不能为空","10201");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("isHot",1);
        map.put("isChina",type);
        try {
            List<ItripAreaDic> list = itripAreaDicService.getItripAreaDicListByMap(map);
            List<ItripAreaDicVO> dicVOList = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(list)){
                //copy数据
                ItripAreaDicVO itripAreaDicVO = null;
                for(ItripAreaDic itripAreaDic : list){
                    itripAreaDicVO = new ItripAreaDicVO();
                    BeanUtils.copyProperties(itripAreaDic,itripAreaDicVO);
                    //判断对象属性是否一样
                    dicVOList.add(itripAreaDicVO);
                }
            }
            return DtoUtil.returnDataSuccess(dicVOList);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("国家type类型不能为空","10201");
        }

    }

    /**
     * 根据城市查询商圈
     * @param cityId
     * @return
     */
    @RequestMapping(value = "/querytradearea/{cityId}",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<ItripAreaDicVO> queryTradeArea(@PathVariable Integer cityId){

        if(cityId == null || cityId == 0){
            return DtoUtil.returnFail("国家type类型不能为空","10203");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("isTradingArea",1);
        map.put("parent",cityId);
        try {
            List<ItripAreaDic> result = itripAreaDicService.getItripAreaDicListByMap(map);
            List<ItripAreaDicVO> output = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(result)){
                //copy数据
                ItripAreaDicVO itripAreaDicVO = null;
                for(ItripAreaDic itripAreaDic : result){
                    itripAreaDicVO = new ItripAreaDicVO();
                    BeanUtils.copyProperties(itripAreaDic,itripAreaDicVO);
                    //判断对象属性是否一样
                    output.add(itripAreaDicVO);
                }
            }
            return DtoUtil.returnDataSuccess(output);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("国家type类型不能为空","10204");
        }

    }

    /**
     * 根据酒店ID查询酒店图片
     * @param targetId
     * @return
     */
    @RequestMapping(value = "/getimg/{targetId}",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<ItripImageVO> getImg(@PathVariable String targetId){

        if(EmptyUtils.isEmpty(targetId)){
            return DtoUtil.returnFail("酒店id不能为空","100213");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("type","0");
        map.put("targetId",Long.valueOf(targetId));
        try {
            List<ItripImage> list = itripImageService.getItripImageListByMap(map);
            List<ItripImageVO> outPut = new ArrayList<>();
            if(EmptyUtils.isNotEmpty(list)){
                ItripImageVO itripImageVO = null;
                for (ItripImage itripImage : list){
                    itripImageVO = new ItripImageVO();
                    BeanUtils.copyProperties(itripImage,itripImageVO);
                    outPut.add(itripImageVO);
                }
            }
            return DtoUtil.returnDataSuccess(outPut);

        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常，获取失败","100212");
        }

    }

    /**
     * 根据酒店id查询酒店特色、商圈、酒店名称
     */
    @RequestMapping(value = "/getvideodesc/{hotelId}",method = RequestMethod.GET,produces = "application/json")
    @ResponseBody
    public Dto<HotelVideoDescVO> getVideoDesc(@PathVariable String hotelId){

        if(EmptyUtils.isEmpty(hotelId)){
            return DtoUtil.returnFail("酒店id不能为空","100215");
        }
        try {
            HotelVideoDescVO hotelVideoDescVO = itripHotelService.getVideoDescByHotelId(Long.valueOf(hotelId));
            return DtoUtil.returnDataSuccess(hotelVideoDescVO);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("系统异常，获取失败","100214");
        }
    }

}
